// packageH/newDraw/newDrawRecommentReward/newDrawRecommentReward.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showAddress:false,
    showAddrss: false,
    activityInfo: {},
    activity_id: 0,
    reward_data_id: 0, //奖励商品记录ID
    list: [],
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0 //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.luck-draw.frontend.index.recommendList');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
            list: res.data.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  //获取更多数据
  _getMoreData() {
    const that = this;
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss('plugin.luck-draw.frontend.index.recommendList');
      app._postNetWork({
        url: {},
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              list: that.data.list.concat(res.data.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  },
  addActivity() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.luck-draw.frontend.team.getActivity');
    app._postNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.activity_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            activityInfo: res.data,
            showAddrss: true
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  // 获取收货地址
  getAddress() {
    this.setData({
      addressData: [],
      showAddress:true
    });
  },
  confirmSelectAddress(e){
    this.setData({
      ['activityInfo.address']: e.detail,
      showAddress:false
    });
  },
  joinActivity() {
    let that = this;
    let json = {
      reward_data_id: that.data.reward_data_id,
      address: JSON.stringify(this.data.activityInfo.address)
    };
    
    if (this.data.activityInfo.type == 2 && this.data.activityInfo.need_address == 1) {
      //虚拟商品并且开启不填写地址
      if(this.data.tel == ''){
        wx.showToast({
          title: '请填写手机号',
          duration: 1000,
          icon: 'none'
        });
        return;
      }
      json.address = JSON.stringify({
        mobile: this.data.tel
      });
    }else{
      if(Object.keys(this.data.activityInfo.address).length == 0){
        wx.showToast({
          title: '请选择收货地址',
          duration: 1000,
          icon: 'none'
        });
        return;
      }
    }
    let urlStr = app.getNetAddresss('plugin.luck-draw.frontend.team.rewardJoinActivity');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        wx.showToast({
          title: res.msg,
          duration: 1000,
          icon: 'none'
        });
        if (res.result == 1) {
          that.getData();
          that.setData({
            showAddrss: false
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  cloneAdress() {
    this.setData({
      showAddrss: false
    });
  },
  confirmBtn(e) {
    let index = e.currentTarget.dataset.index;
    let order = e.currentTarget.dataset.item;

    this.data.activity_id = order.activity_id;
    this.data.reward_data_id = order.reward_data_id;
    if (index == 0) {
      //当奖励有商品时。填写地址参加
      this.addActivity();
    } else {
      //查看订单详情
      if (order.order_id) {
        wx.navigateTo({
          url: '/packageA/member/orderdetail/orderdetail?order_id=' + order.order_id + '&orderType=luck-draw'
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});